<? 
	include('base.php');
	User::protect();
	include_class('releases');
	include_class('m2');
	include_class('release_tracks');
	$rt = ReleaseTrack::get($_GET['id']);
	$section='releases';
	if (!db::isError($rt)) {
		$r = $rt->getReleaseObject();
		switch($_GET['task']) {
			case 'update':
				$res = $rt->update($_POST);
				if (!db::isError($res)) {
					header('Location: release_track_edit.php?id=' . $_GET['id']);
				}
				break;
			case 'delete':
				// returns the album this is from
				$res = $rt->remove();
				if (!db::isError($res)) {
					header('Location: release_edit.php?id=' . $res->getID());
					exit;
				}
				break;
			case 'remove_audio':
				// returns the album this is from
				$res = $rt->removeAudio();
				if (!db::isError($res)) {
					header('Location: release_track_edit.php?id=' . $_GET['id']);
					exit;
				}
				break;
		}
	}
	$editors = array('lyrics','meta_information');
	$page_title = 'Edit Track';
	include('layout/header.php');
	

if (!db::isError($rt) && !db::isError($r)) { ?>

<div id="breadcrumb">
	<a href="index.php">Audition &#62;</a>
	<a href="releases.php">Releases &#62;</a>
	<a href="release_edit.php?id=<?=$r->getID()?>">View/Edit Release &#62;</a>
	View/Edit Release Track &#62;
</div>


	<? if (User::isAdmin() && (($_GET['task'] == 'edit') || ($_GET['task'] == 'update'))) {
		
		if (db::isError($res)) {
			$res->outputList();
		}
		
		$number = $_POST ? $_POST['number'] : $rt->getNumber();
		$title = $_POST ? $_POST['title'] : $rt->getTitle();
		$lyrics = $_POST ? $_POST['lyrics'] : $rt->getLyrics();
		$meta_information = $_POST ? $_POST['meta_information'] : $rt->getMetaInformation();
		if ($_POST) {
			$length = $_POST['length'];
		} else {
			$length = ($rt->lengthSet()) ? $rt->getLength() : '';
		}
				
		?>
		
		<h1>edit track:</h1>
		<div class="inset">
		<form id="edit_entry" action="<?=$PHP_SELF?>?task=update&amp;id=<?=$_GET['id']?>" method="post">
		<table border="0" class="edit-form" cellspacing="0" cellpadding="0">
		<tr>
			<td width="25%"><strong>Track #</strong><br/>
			<input type="text" name="number" value="<?=$number?>" class="text"></td>
			<td width="25%"><strong>Length (m:s)</strong><br/>
			<input type="text" name="length" value="<?=$length?>" class="text"></td>
			<td width="50%"><strong>Title</strong><br/>
			<input type="text" name="title" value="<?=$title?>" class="text"></td>
		</tr>
		</table>
		<div class="edit-form">
			<strong>Lyrics:</strong><br/>
			<textarea name="lyrics" id="lyrics" class="full"><?=$lyrics?></textarea>
		</div>
		<div class="edit-form">
			<strong>Meta Information</strong><br/>
			<textarea name="meta_information" id="meta_information" class="full"><?=$meta_information?></textarea>
		</div>
		<div class="edit-form">
			<input type="submit" value="Update Track" class="button" name="submit" class="button">
			<input type="submit" value="Exit Without Updating" class="button" name="preview" onclick="location.href='<?=$PHP_SELF?>?id=<?=$rt->getID()?>'; return false" class="button">
		</div>
		</div>
		
	<? } else { ?>

		<h1>view track:</h1>
		<div class="inset">
		
		<h2><?=$rt->getNumber()?>. <?=$rt->getTitle()?> <?
			if ($rt->lengthSet()) {
				echo('('.$rt->getLength().')');
			}
		?></h2>
		
		<br>
		
		<strong>Lyrics</strong><br>
		<? if ($rt->getLyrics()) { 
			echo($rt->getLyrics());
		} else { ?>
			None
		<? } ?>
			
		<br><br>
		
		<strong>Meta Information</strong><br>
		<? if ($rt->getMetaInformation()) { ?>
			<?=nl2br($rt->getMetaInformation())?>
		<? } else { ?>
			None
		<? } ?>
		
		<br/><br/>

		<strong>Audio File</strong><br>
		<? if ($rt->getAudioFile()) { ?>
			<?=$rt->getAudioFile()?><br>
			<a href="release_track_audio_dl.php?id=<?=$rt->getID()?>">Listen</a>
			<? if (User::isAdmin()) { ?>
				| <a href="<?=$_SERVER['PHP_SELF']?>?id=<?=$_GET['id']?>&amp;task=remove_audio">Remove</a>
			<? } ?>
		<? } else { ?>
			None Set.<br>
			<? if (User::isAdmin()) { ?>
				(<a href="release_track_audio_add.php?id=<?=$_GET['id']?>">Add Audio File</a>)
			<? } ?>
		<? } ?>
		
		</div>
		<form name="entry_status" method="get">
			<? if (User::isAdmin()) { ?>
				<input type="submit" name="" value="Edit Track" onclick="location.href='<?=$PHP_SELF?>?task=edit&id=<?=$_GET['id']?>';return false" class="button">
				<input type="submit" value="Remove Track" name="" onclick="if (confirm('Are you sure you wish to delete this track?')) { location.href='<?=$PHP_SELF?>?task=delete&id=<?=$_GET['id']?>'};return false" class="button">
			<? } ?>
			<input type="button" name="" value="Audio/Video" onclick="location.href='release_track_media.php?id=<?=$_GET['id']?>';return false" class="button">

			<input type="submit" value="Return to Release" name="" onclick="location.href='release_edit.php?id=<?=$r->getID()?>'; return false" class="button">
		</form>
		
		</div>
	<? } ?>

<? } else { ?>


<div id="breadcrumb">
	<a href="index.php">Audition &#62;</a>
	<a href="releases.php">Releases &#62;</a>
	Invalid Entry
</div>

<? if (db::isError($rt)) {
	$rt->outputDialog('Return to Releases','releases.php');
} else if (db::isError($r)) {
	$r->outputDialog('Return to Releases','releases.php');
}

}

include('layout/footer.php'); ?>
